<template>
  <VueFlow v-model="elements" fit-view-on-init>
    <!-- bind your custom node type to a component by using slots, slot names are always `node-<type>` -->
    <!-- <template #node-special="specialNodeProps">
      <SpecialNode v-bind="specialNodeProps" />
    </template> -->

    <!-- bind your custom edge type to a component by using slots, slot names are always `edge-<type>` -->
    <!-- <template #edge-special="specialEdgeProps">
      <SpecialEdge v-bind="specialEdgeProps" />
    </template> -->
    <Panel>
      <button type="button" @click="onAddNode">Add a node</button>
      <button type="button" @click="onAddNodes">Add multiple nodes</button>
    </Panel>
  </VueFlow>
</template>

<script lang="ts">
export default {
  name: 'HsVueFlow',
}
</script>
<script lang="ts" setup>
import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
import type { Elements } from '@vue-flow/core'

/* import the necessary styles for Vue Flow to work */
import '@vue-flow/core/dist/style.css'
/* import the default theme, this is optional but generally recommended */
import '@vue-flow/core/dist/theme-default.css'

const { addNodes } = useVueFlow()

const elements = ref<Elements>([
  // nodes

  // an input node, specified by using `type: 'input'`
  { id: '1', type: 'input', label: 'Node 1', position: { x: 250, y: 5 } },

  // default node, you can omit `type: 'default'` as it's the fallback type
  { id: '2', label: 'Node 2', position: { x: 100, y: 100 } },

  // An output node, specified by using `type: 'output'`
  { id: '3', type: 'output', label: 'Node 3', position: { x: 400, y: 200 } },

  // edges

  // simple default bezier edge
  // consists of an id, source-id and target-id
  { id: 'e1-3', source: '1', target: '3' },

  // an animated edge, specified by using `animated: true`
  { id: 'e1-2', source: '1', target: '2', animated: true },
])

function generateRandomNode() {
  return {
    id: Math.random().toString(),
    position: { x: Math.random() * 500, y: Math.random() * 500 },
    label: 'Random Node',
    data: {
      hello: 'world',
    },
  }
}

function onAddNode() {
  // add a single node to the graph
  addNodes(generateRandomNode())
}

function onAddNodes() {
  // add multiple nodes to the graph
  addNodes(Array.from({ length: 2 }, generateRandomNode))
}
</script>

<style lang="scss" scoped></style>
